#include <iostream>
#include "thread.h"
#include "log.h"
#include "util.h"

namespace johnsonli
{
	//局部线程静态变量，只在当前线程有用
	//当前运行的线程
	static thread_local Thread* t_thread = nullptr;
	//当前运行的线程的名称
	static thread_local std::string t_thread_name = "UNKNOW";

	static johnsonli::Logger::ptr g_logger = LOG_NAME("system");

	Thread* Thread::GetThis() {
		return t_thread;
	}

	const std::string& Thread::GetName() {
		return t_thread_name;
	}

	void Thread::SetName(const std::string& name) {
		if (name.empty()) {
			return;
		}
		if (t_thread) {
			t_thread->m_name = name;
		}
		t_thread_name = name;
	}

	Thread::Thread(std::function<void()> cb, const std::string& name)
		: m_cb(cb)
		, m_name(name)
	{
		if (name.empty())
		{
			m_name = "UNKNOW";
		}
		int rt = pthread_create(&m_thread, nullptr, &Thread::run, this);
		if (rt) {
			LOG_ERROR(g_logger) << "pthread_create thread fail, rt=" << rt
				<< " name=" << name;
			throw std::logic_error("pthread_create error");
		}

		//等待，直到创建出的线程开始执行，run()
		m_semaphore.wait();
	}

	Thread::~Thread() {
		if (m_thread) {
			pthread_detach(m_thread);
		}
	}

	void Thread::join() {
		if (m_thread) {
			int rt = pthread_join(m_thread, nullptr);
			if (rt) {
				LOG_ERROR(g_logger) << "pthread_join thread fail, rt=" << rt
					<< " name=" << m_name;
				throw std::logic_error("pthread_join error");
			}
			//m_thread = 0;
		}
	}

	void* Thread::run(void* arg) {
		Thread* thread = (Thread*)arg;
		t_thread = thread;
		t_thread_name = thread->m_name;
		thread->m_id = johnsonli::getThreadId();
		pthread_setname_np(pthread_self(), thread->m_name.substr(0, 15).c_str());

		std::function<void()> cb;
		cb.swap(thread->m_cb);

		//线程初始化完成后，就唤醒 Thread()，完成构造
		thread->m_semaphore.notify();

		cb();
		return 0;
	}

}